<template>
	<view class="container">
		<u-toast ref="uToast"></u-toast>
		<view class="wait" v-if="draw.state == 10">
			<view class="cover">
				<view class="plate">
					<view class="loading-icon">
						<u-icon size="61"
							name="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/63889c845a7e3f0310de5223/6398219cd109e60012196b2c/16715997159036091118.png">
						</u-icon>
					</view>
					<view class="loading-tips">
						Ai正在拼命绘制中... 剩余时间{{ draw.waiting }}
					</view>
				</view>
			</view>
			<view class="tips">
				<view class="text">AI努力绘制中...</view>
				<view class="text remind"><text>返回/退出后</text> 稍后可在我的->我的绘画中查看此图</view>
			</view>
			<!-- 			<view class="notice">
				<u-button iconColor="#fff" color="#F22E38" icon="volume" text="订阅通知，绘制完成后将推送消息通知您"></u-button>
			</view> -->
		</view>
		<view class="complete" v-if="draw.state == 20">
			<view class="cover" @click="onPreviewImage">
				<u--image border-radius="15" :src="draw.draw_image_url" width="100%" height="720rpx" mode="scaleToFill">
				</u--image>
			</view>
			<view class="user">
				<view class="left">
					<view class="avatar">
						<u--image :src="userInfo.avatar_id?userInfo.avatar.file_path:userInfo.avatarUrl" shape="circle"
							width="61px" height="61px"></u--image>
					</view>
					<view class="info">
						<view class="title">{{ userInfo.nickName }}</view>
						<view class="desc">绘画ID：{{ task_id }}</view>
					</view>
				</view>
				<view class="right" @click="onApplyCover" v-if="setting.is_apply_cover == 10">
					<u-button color="#F22E38" text="申请AI封面"></u-button>
				</view>
			</view>
			<view class="keywords" v-if="setting.is_copy_ad == 10">
				<view class="content">
					{{ draw.draw_text }}
				</view>
			</view>
			<view class="label">
				<view class="style">
					<view class="i">风格</view>
					<view class="text">{{ draw.draw_style }}&{{ draw.draw_size }}</view>
				</view>
				<view class="date">
					<view class="text">{{ draw.create_time }}</view>
				</view>
			</view>
			<view class="btn-group">
				<view class="btn" @click="onSaveImage">
					<u-button shape="circle" color="#F22E38" text="快速下载绘画"></u-button>
				</view>
				<view class="btn" @click="onCopyData">
					<u-button shape="circle" :plain="true" :hairline="true" color="#F22E38" text="复制关键词"></u-button>
				</view>
				<view class="btn">
					<u-button open-type="share" shape="circle" :plain="true" :hairline="true" color="#F22E38"
						text="分享绘画"></u-button>
				</view>
			</view>
			<!-- 			<view class="action">
				<view class="action-group">
					<view class="btn weixin">
						<view class="grid">
							<u-icon size="38"
								name="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/63889c845a7e3f0310de5223/6398219cd109e60012196b2c/16716068583077889338.png"
								:size="22">
							</u-icon>
							<text class="grid-text">分享好友</text>
						</view>
					</view>
					<view class="btn poster">
						<u-button shape="circle" :plain="true" :hairline="true" color="#F22E38" text="生成海报"></u-button>
					</view>
					<view class="btn save">
						<u-button shape="circle" :plain="true" :hairline="true" color="#F22E38" text="保存作品"></u-button>
					</view>
				</view>
			</view> -->
		</view>
		<!-- 作品隐私状态  - 自己可见 -->
		<view class="privacy-btn" v-if="userInfo && userInfo.user_id == draw.user_id" @click="onSetOpen">
			<view class="btn" v-if="draw.is_open == 10">
				公开
			</view>
			<view class="btn" v-if="draw.is_open == 20">
				私密
			</view>
		</view>
		<!-- 底部流量主 -->
		<view class="ad" v-if="spool.video.status == 10">
			<ad :unit-id="spool.video.id" ad-type="video" ad-theme="white"></ad>
		</view>
	</view>
</template>

<script>
	import {
		queue,
		set_open
	} from '@/api/draw.js';
	import {
		detail
	} from '@/api/user.js';
	import {
		config
	} from '@/api/system.js';
	import {
		insert,
		rewarded
	} from '@/utils/ad.js';
	export default {
		data() {
			return {
				spool: {},
				userInfo: {},
				draw: {},
				state: 1,
				timer: null,
				task_id: '',
				share: {},
				setting: {},
				spool: {}
			};
		},
		onLoad(options) {
			this.task_id = options.task_id
			this.timer = setInterval(() => {
				this.getQueue()
			}, 1000)
			this.getUser()
			this.getShare()
			this.getSpool()
			this.getSetting()
		},
		onShareTimeline() {
			let _this = this
			return {
				imageUrl: _this.draw.draw_image_url,
				title: _this.draw.draw_text
			}
		},
		onShareAppMessage() {
			let _this = this
			return {
				imageUrl: _this.draw.draw_image_url,
				desc: _this.draw.draw_text,
				title: _this.share.title,
				path: '/pages/draw/detail/index?task_id=' + _this.draw.task_id
			}
		},
		methods: {
			getSpool() {
				let _this = this;
				config({
					key: 'spool'
				}).then(res => {
					_this.spool = res.data.config
					if (_this.spool.insert.status == 10) {
						insert.load(_this.spool.insert.id)
						insert.show()
					}
					if (_this.spool.reward.status == 10) {
						rewarded.load(_this.spool.reward.id, () => {
							uni.setClipboardData({
								data: _this.draw.draw_text
							})
						})
					}
				}).catch(err => {

				})
			},
			onApplyCover() {
				uni.navigateTo({
					url: '/pages/cover/apply/index'
				})
			},
			getSetting() {
				let _this = this;
				config({
					key: 'draw'
				}).then(res => {
					_this.setting = res.data.config
				}).catch(err => {

				})
			},
			getShare() {
				config({
					key: 'share'
				}).then(res => {
					this.share = res.data.config
				}).catch(err => {

				})
			},
			onSetOpen() {
				let _this = this
				set_open({
					task_id: this.draw.task_id
				}).then(res => {
					if (res.code == 1) {
						_this.$refs.uToast.show({
							message: "设置成功~！",
							complete() {
								_this.getQueue()
							}
						})
					}
				}).catch(err => {

				})
			},
			onSaveImage() {
				let _this = this
				uni.downloadFile({
					url: this.draw.draw_image_url, //仅为示例，并非真实的资源
					success: (res) => {
						if (res.statusCode === 200) {
							console.log('下载成功');
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									_this.$refs.uToast.show({
										message: "绘画下载成功~！",
										complete() {

										}
									})
									console.log('save success');
								}
							})
						}
					}
				});
			},
			onCopyData() {
				let _this = this
				if (_this.setting.is_copy_ad == 20) {
					uni.showModal({
						title: '温馨提示',
						content: '复制关键词需要观看一段广告，是否确认继续？',
						success: (res) => {
							if (res.confirm) {
								rewarded.show()
							}
						}
					})
				} else {
					uni.setClipboardData({
						data: _this.draw.draw_text
					})
				}
			},
			onPreviewImage() {
				uni.previewImage({
					urls: [this.draw.draw_image_url],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			getUser() {
				detail({}).then(res => {
					this.userInfo = res.data.userInfo
				}).catch(err => {

				})
			},
			getQueue() {
				queue({
					task_id: this.task_id
				}).then(res => {
					this.draw = res.data.draw
					if (res.data.draw.state == 20) {
						clearInterval(this.timer)
					}
				}).catch(err => {

				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f4f4f4;
	}


	@keyframes xing {
		0% {
			transform: scale(1);
		}

		25% {
			transform: scale(1.2);
		}

		50% {
			transform: scale(1);
		}

		75% {
			transform: scale(1.2);
		}
	}

	.privacy-btn {
		position: fixed;
		right: 0rpx;
		top: 30%;
		right: 30rpx;

		.btn {
			background-color: #fa3534;
			box-shadow: 0rpx 0rpx 30rpx #dd6161;
			color: #fff;
			width: 100rpx;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			border-radius: 50%;
			z-index: 999;
			-webkit-animation-name: xing;
			-webkit-animation-timing-function: ease-in-out;
			-webkit-animation-iteration-count: infinite;
			-webkit-animation-duration: 2s;
		}
	}

	@keyframes fadenum {
		100% {
			transform: rotate(360deg);
		}
	}

	.ad {
		margin-top: 30rpx;
	}

	.complete {
		.action {
			width: 100%;
			position: fixed;
			bottom: 0rpx;
			left: 0rpx;
			background-color: #fff;
			padding: 30rpx 10rpx;

			.action-group {
				width: 90%;
				margin: 0 auto;
				display: flex;
				flex-direction: row;
				justify-content: space-between;

				.weixin {
					width: 40%;
					display: flex;
					flex-direction: row;
					justify-content: flex-start;

					.grid {
						display: flex;
						flex-direction: column;
						align-items: center;

						.grid-text {
							font-size: 24rpx;
							margin-top: 10rpx;
						}
					}
				}

				.poster {
					width: 28%;
				}

				.save {
					width: 28%;
				}
			}
		}

		.btn-group {
			width: 90%;
			margin: 0 auto;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 15rpx;
			background-color: #fff;
			padding: 30rpx 10rpx;
			border-radius: 10rpx;

			.btn {
				.u-button {
					height: 84rpx;
				}
			}
		}

		.label {
			width: 92%;
			margin: 0 auto;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			margin-top: 15rpx;

			.date {
				.text {
					background-color: #FFDEE0;
					color: #F22E38;
					padding: 5rpx 10rpx;
				}
			}

			.style {
				display: flex;
				flex-direction: row;
				align-items: center;

				.i {
					background-color: #FFDEE0;
					color: #F22E38;
					padding: 5rpx 10rpx;
					border-radius: 5rpx;
					margin-right: 15rpx;
				}

				.text {
					font-size: 24rpx;
					font-weight: bolder;
					color: #F22E38;
				}
			}
		}

		.keywords {
			width: 90%;
			margin: 0 auto;
			background-color: #fff;
			padding: 15rpx;
			border-radius: 10rpx;
			margin-top: 15rpx;

			.content {
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 3;
				/*这里设置几行*/
				overflow: hidden;
			}
		}

		.user {
			width: 90%;
			margin: 0 auto;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			padding: 15rpx;
			border-radius: 10rpx;

			.left {
				width: 70%;
				display: flex;
				flex-direction: row;
				align-items: center;

				.avatar {
					margin-right: 15rpx;
				}

				.info {
					.title {
						font-weight: bolder;
					}

					.desc {
						margin-top: 5rpx;
						font-size: 24rpx;
					}
				}
			}

			.right {
				width: 30%;
			}
		}

		.cover {
			width: 94%;
			margin: 15rpx auto;
			border-radius: 15rpx;
			overflow: hidden;
		}
	}

	.wait {
		.notice {
			width: 80%;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.tips {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.text {
				margin-top: 15rpx;
			}

			.remind {
				font-size: 24rpx;
				color: #999;

				text {
					color: #F22E38;
					margin-right: 15rpx;
					font-weight: bolder;
				}
			}
		}

		.cover {
			width: 100%;
			height: 720rpx;
			border-radius: 10rpx;
			background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/63889c845a7e3f0310de5223/6398219cd109e60012196b2c/16715997158982711362.png');
			background-size: 100% 100%;
			background-repeat: no-repeat;

			.plate {
				width: 100%;
				height: 720rpx;
				backdrop-filter: blur(25rpx);
				background: rgba(255, 255, 255, .3);
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.loading-icon {
					animation: fadenum 5s infinite;
				}

				.loading-tips {
					margin-top: 30rpx;
					color: #fff;
				}
			}
		}
	}
</style>
